package com.unittec.drainage.provider.auth.controller;

import com.unittec.drainage.provider.auth.entity.SysFileEntity;
import com.unittec.drainage.provider.auth.service.SysFileService;
import com.unittec.drainage.provider.log.Log;
import com.unittec.drainage.provider.utils.CopyUtils;
import com.unittec.drainage.sdk.auth.request.Req101000;
import com.unittec.drainage.sdk.root.domain.GatewayRequest;
import com.unittec.drainage.sdk.root.domain.Result;
import com.unittec.drainage.sdk.root.enums.BizCode;
import com.unittec.drainage.sdk.root.enums.BizCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wuxinwei
 */
@RestController
@Api(tags = "系统文件管理")
public class SysFileController {

    @Autowired
    private SysFileService sysFileService;

    @Autowired
    Log log;

    @RequestMapping(value = "/101000", method = RequestMethod.POST)
    @ApiOperation(value = "保存文件", notes = "保存文件")
    @ResponseBody
    public Result saveFile(@RequestBody GatewayRequest<Req101000> req) {
        log.debug(req, "saveFile {}  ", "begin");
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        try {
            SysFileEntity entity = new SysFileEntity();
            CopyUtils.copyProperties(req.getContent(), entity);
            sysFileService.save(entity);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "saveFile {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            return Result.success(req);
        } else {
            return Result.error(req, bizCodeEnum);
        }
    }

}
